﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.DAL;
using SCS.Common.Models;


namespace SCS.Business
{
    public class ClassServices
    {
        IUnitOfWork _unitOfWork;
        /// <summary>
        /// Constructor
        /// </summary>
        public ClassServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// Count Number Class
        /// </summary>
        /// <param name="searchStringID">search text</param>
        /// <returns>number of Class</returns>
        public int CountClass(string searchStringID)
        {
            if (string.IsNullOrEmpty(searchStringID))
            {
                return _unitOfWork.Class.GetCount();
            }
            else
            {
                return _unitOfWork.Class.GetSearchCount(searchStringID);
            }
        }

        /// <summary>
        /// Get A Class In School Year
        /// </summary>
        /// <param name="classID">class ID</param>
        /// <param name="schoolyearID">schoolyear ID</param>
        /// <returns>Class In School Year</returns>
        public ClassInSchoolYear GetClassInSchoolYear(int classID,int schoolyearID)
        {
            return _unitOfWork.ClassInSchoolYear.Get(m => m.classID == classID && m.schoolYearID == schoolyearID).ToList().FirstOrDefault();
        }

        /// <summary>
        /// Get list class in school year
        /// </summary>
        /// <param name="schoolyearID">schoolyear ID</param>
        /// <returns>All class in school year</returns>
        public IEnumerable<ClassInSchoolYear> GetAllClassInSchoolYear(int schoolyearID)
        {
            return _unitOfWork.ClassInSchoolYear.Get(m=>m.schoolYearID == schoolyearID );
        }

        /// <summary>
        /// Get Start Date
        /// </summary>
        /// <param name="startdate">start date</param>
        /// <returns>A class</returns>
        public Class GetStartDate(DateTime startdate)
        {
            return _unitOfWork.Class.Get(m => m.startDate == startdate).ToList().FirstOrDefault();
        }

        /// <summary>
        /// GetClassPaging
        /// </summary>
        /// <param name="pageNumber">pageNumber</param>
        /// <param name="pageSize">pageSize</param>
        /// <returns>Class list</returns>
        public IEnumerable<Class> GetClassPaging(int pageNumber, int pageSize)
        {
            return _unitOfWork.Class.GetClassPaging(pageNumber, pageSize);
        }

        /// <summary>
        /// SearchClassPaging
        /// </summary>
        /// <param name="pageNumber">pageNumber</param>
        /// <param name="pageSize">pageSize</param>
        /// <param name="searchStringID">string</param>
        /// <returns>class list</returns>
        public IEnumerable<Class> SearchClassPaging(int pageNumber, int pageSize, string searchStringID)
        {
            return _unitOfWork.Class.SearchClassPaging(pageNumber, pageSize, searchStringID);
        }

        /// <summary>
        /// add a class
        /// </summary>
        /// <param name="clas">Class</param>
        /// <param name="classInSchoolYear">ClassInSchoolYear</param>
        /// <returns>true false</returns>
        public bool AddClass(Class clas, ClassInSchoolYear classInSchoolYear)
        {
            try
            {
                int id1 = _unitOfWork.Class.GetAll().Max(t => (int?)t.classID) ?? 0;
                clas.classID = id1 + 1;
                classInSchoolYear.classID = clas.classID;
                
                int id2 = _unitOfWork.ClassInSchoolYear.GetAll().Max(a => (int?)a.classInSchoolYearID) ?? 0;
                classInSchoolYear.classInSchoolYearID = id2 + 1;

                _unitOfWork.ClassInSchoolYear.Add(classInSchoolYear);
                _unitOfWork.Class.Add(clas);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// UpdateCLass
        /// </summary>
        /// <param name="clas">Class</param>
        /// <param name="classInSchoolYear">ClassInSchoolYear</param>
        /// <returns>bool</returns>
        public bool UpdateCLass(Class clas, ClassInSchoolYear classInSchoolYear)
        {
            try
            {
                _unitOfWork.Class.Update(clas);
                _unitOfWork.ClassInSchoolYear.Update(classInSchoolYear);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// DeleteClass
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="schoolyearID">schoolyearID</param>
        /// <returns>bool</returns>
        public bool DeleteClass(int id, int schoolyearID)
        {
            try
            {
                //Class clas = new Class();
                ClassInSchoolYear classInSchoolYear = new ClassInSchoolYear();
                //clas = GetClass(id);
                classInSchoolYear = GetClassInSchoolYear(id, schoolyearID);
                //_unitOfWork.Class.Delete(clas);
                _unitOfWork.ClassInSchoolYear.Delete(classInSchoolYear);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// AssignSubject
        /// </summary>
        /// <param name="assginsubject">TeachingAssignment</param>
        /// <returns>bool</returns>
        public bool AssignSubject(TeachingAssignment assginsubject)
        {
            try
            {
                int id = _unitOfWork.TeachingAssignment.GetAll().Max(t => (int?)t.teachingAssignmentID) ?? 0;
                assginsubject.teachingAssignmentID = id + 1;
                _unitOfWork.TeachingAssignment.Add(assginsubject);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// count
        /// </summary>
        /// <returns>1</returns>
        public int GetCount()
        {
            return 1;
        }

        /// <summary>
        /// GetClass
        /// </summary>
        /// <param name="classID">classID</param>
        /// <returns>class</returns>
        public Class GetClass(int classID )
        {
            return _unitOfWork.Class.Get(m => m.classID == classID).LastOrDefault();
        }

        /// <summary>
        /// GetAllTeacher
        /// </summary>
        /// <returns>teacher list</returns>
        public IEnumerable<Teacher> GetAllTeacher()
        {
            return _unitOfWork.Teacher.GetAll();
        }

        /// <summary>
        /// GetAllSchoolYear
        /// </summary>
        /// <returns>school year list</returns>
        public IEnumerable<SchoolYear> GetAllSchoolYear()
        {
            return _unitOfWork.SchoolYear.GetAll();
        }

        /// <summary>
        /// GetAllSubject
        /// </summary>
        /// <returns>subject list</returns>
        public IEnumerable<Subject> GetAllSubject()
        {
            return _unitOfWork.Subject.GetAll();
        }

        /// <summary>
        /// GetAllClassInSchoolYears
        /// </summary>
        /// <returns>class list</returns>
        public IEnumerable<ClassInSchoolYear> GetAllClassInSchoolYears()
        {
            int maxSchoolYear = _unitOfWork.SchoolYear.GetAll().Select(m => m.schoolYearID).Max();
            return _unitOfWork.ClassInSchoolYear.Get(m => m.schoolYearID == maxSchoolYear);
        }
        /// <summary>
        /// GetAllClassInSchoolYears
        /// </summary>
        /// <returns>class list</returns>
        public List<TeachingAssignment> GetTeachingAssign(int classID)
        {
            int maxSchoolYear = _unitOfWork.SchoolYear.GetAll().Select(m => m.schoolYearID).Max();
            return _unitOfWork.TeachingAssignment.Get(m => m.schoolYearID == maxSchoolYear && m.classID == classID).ToList();
        }

        /// <summary>
        /// GetAllClassInSchoolYears
        /// </summary>
        /// <returns>class list</returns>
        public void DeleteTeachingAssign(int teachingAssignID)
        {
            TeachingAssignment item = _unitOfWork.TeachingAssignment.Get(m => m.teachingAssignmentID == teachingAssignID).FirstOrDefault();
            _unitOfWork.TeachingAssignment.Delete(item);
            _unitOfWork.Commit();
        }
        /// <summary>
        /// AssignSubject
        /// </summary>
        /// <param name="assginsubject">TeachingAssignment</param>
        /// <returns>bool</returns>
        public void InsertTeachingAssignment(int classID,byte? subjectID,int? teacherID)
        {
            try
            {
                TeachingAssignment assginsubject = new TeachingAssignment();
                int id = _unitOfWork.TeachingAssignment.GetAll().Max(t => (int?)t.teachingAssignmentID) ?? 0;
                assginsubject.teachingAssignmentID = id + 1;
                int maxSchoolYear = _unitOfWork.SchoolYear.GetAll().Select(m => m.schoolYearID).Max();
                assginsubject.schoolYearID = maxSchoolYear;
                if (subjectID.HasValue)
                {
                    assginsubject.subjectID = subjectID.Value;
                }
                if (teacherID.HasValue)
                {
                    assginsubject.teacherID = teacherID.Value;
                }
                assginsubject.classID = classID;
                _unitOfWork.TeachingAssignment.Add(assginsubject);
                _unitOfWork.Commit();
            }
            catch (Exception e)
            {
            }
        }
    }
}
